<?php
namespace App\Admin\Http\Controllers\Web;

use App\Admin\Eloquent\Code;
use App\Admin\Eloquent\GiftReceive;
use App\Admin\Http\Controllers\Controller;
use App\Eloquent\TradeRecords;
use App\Admin\Eloquent\Users;
use Illuminate\Http\Request;
use App\Extensions\Pagination\BootstrapThreePresenter;
use QueryHostingDeposit;
use QueryHostingWithdraw;

//财务管理
class FicoController extends Controller
{
    /*
     * 交易记录
     */
    public function index(Request $request,string $download = ''){

        $query=$request->only('start_at','end_at','treade_cate_id','name','treade_status');

        $list=TradeRecords::query();
        //用户名或名字
        if(!empty($query['name'])){
            $list=$list->where('user_info.name','=',$query['name'])->orWhere('users.username','=',$query['name']);
        }
        //交易类型
        if(!empty($query['treade_cate_id'])){
            $list=$list->where('trade_records.type','=',$query['treade_cate_id']);
        }else{
            $type=array('4','5');
            $list=$list->whereIn('trade_records.type',$type);
        }
        //开始时间
        if(!empty($query['start_at'])){
            $list=$list->where('trade_records.pay_time','>',$query['start_at']);
        }
        //结束时间
        if(!empty($query['end_at'])){
            $list=$list->where('trade_records.pay_time','<',$query['end_at']);
        }
        if(!empty($query['treade_status']) || $query['treade_status']=='0' ){
            $list=$list->where('trade_records.status','=',$query['treade_status']);
        }
        $list=$list->join('user_info','user_info.user_id','=','pay_user')
            ->join('users','users.id','=','pay_user')
            ->orderBy('trade_records.pay_time','desc')
            ->select('users.username','users.phone_number','user_info.name','user_info.bank_code','trade_records.*');

        if($download == 'download'){
            $data = $list->get();
            qeury_export_excel('交易记录',$data);
        }
        $fee_list=clone $list;
        $pagelist = $list->paginate(15);
        $fee_money=$fee_list->where('status','=','1')->get()->sum('fee_money');
        $list=$pagelist;
        if (!empty($query)) {
            $list->appends($query);
        }

        //银行代码
        $bank_cate=Code::where('type_id','=','bank_code')->get();
        $bank_cate=$bank_cate->keyBy('code');
        $links = (new BootstrapThreePresenter($list))->render();
        return view('admin.web.fico.index',compact('list','bank_cate','links','query','fee_money'));
    }

    /*
     * 红包查询
     */
    public function giftRecords(Request $request,$download=''){
        $query=$request->only('start_at','end_at','activity_type_id','name');

        $list=GiftReceive::query();
        //交易类型
        if(!empty($query['activity_type_id'])){
            $list=$list->where('activity_type','=',$query['activity_type_id']);
        }
        //开始时间
        if(!empty($query['start_at'])){
            $list=$list->where('input_time','>',$query['start_at']);
        }
        //结束时间
        if(!empty($query['end_at'])){
            $list=$list->where('input_time','<',$query['end_at']);
        }

        $list=$list->join('gift_activities','gift_activities.id','=','gift_receives.activity_id')
            ->join('user_info','user_info.user_id','=','gift_receives.user_id')
            ->join('users','users.id','=','gift_receives.user_id')
//            ->where('user_info.name','like','%'.(isset($query['name'])?$query['name']:'').'%')
            ->orderBy('input_time','desc')
            ->select('users.username','users.phone_number','user_info.name','gift_receives.*','gift_activities.name as gift_name');

        if(!empty($query['name'])){
            $list->where('users.username','=',$query['name'])->orWhere('users.phone_number','=',$query['name'])->orWhere('user_info.name','=',$query['name']);
        }
        $r_list=$list->get();
        $r_list=$r_list->groupBy('activity_type');
        $money=array();
        foreach($r_list as $key=> $item){
            $money[$key]['0']=null;
            $money[$key]['1']=null;
            $money[$key]['2']=null;
            foreach($item as $a){
                $money[$key][$a->status][]=$a;
            }
            foreach($money[$key] as $to_key=>$b){
                $money[$key][$to_key]=collect($b)->sum('money');
            }
            $money[$key]['0']=$money[$key]['0']+$money[$key]['1']+$money[$key]['2'];
        }
        if($download == 'download'){
            $data = $list->get();
            qeury_export_excel('红包记录',$data);
        }
        $list=$list->paginate(15);

        if (!empty($query)) {
            $list->appends($query);
        }

        //红包类型
        $gift_cate=Code::where('type_id','=','gift_activity_type')->get();
        $gift_cate=$gift_cate->keyBy('code');
        $links = (new BootstrapThreePresenter($list))->render();
        return view('admin.web.fico.gift',compact('list','links','query','gift_cate','money'));
    }
    /**
     * 交易记录查询
     * @return [type] [description]
     */
    public function checkTrade($id){
        $trade_records=TradeRecords::find($id);
        $user=Users::find($trade_records->pay_user);
        //充值
        if($trade_records->type==4){
            $data=[
                'identity_id'=>$user->identity_id,
                'identity_type'=>'UID',
                'account_type'=>'SAVING_POT',
                'out_trade_no'=>$trade_records->sn,
            ];
            $result=QueryHostingDeposit::send($data);
        }
        //提现
        if($trade_records->type==5){
            $data=[
                'identity_id'=>$user->identity_id,
                'identity_type'=>'UID',
                'account_type'=>'SAVING_POT',
                'out_trade_no'=>$trade_records->sn,
            ];
            $result=QueryHostingWithdraw::send($data);
        }
        dd($result);
    }
}